package com.ecarx.camera.worker.car.aosp;

import android.os.Looper;

import androidx.lifecycle.MutableLiveData;

import java.util.Objects;

/**
 * 去重的liveData
 * <p>author: bin.sun
 * <p>date: 2023/3/9
 */
public class DistinctLiveData<T> extends MutableLiveData<T> {

    public DistinctLiveData(T value) {
        super(value);
    }

    public DistinctLiveData() {
    }

    @Override
    public void setValue(T value) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw new IllegalStateException("Cannot invoke setValue on a background thread");
        }
        T oldValue = getValue();
        if (oldValue == null && value == null) {
            return;
        }
        if (Objects.equals(oldValue, value)) {
            return;
        }
        super.setValue(value);
    }
}
